package com.itboye.api.domain;

import com.github.pagehelper.Page;
import com.itboye.api.model.ByResponseData;
import com.itboye.mybatis.helper.ByPageInfoHelper;
import com.itboye.mybatis.model.ProjectApplyModel;
import com.itboye.mybatis.model.ProjectModel;
import com.itboye.services.recruit.interfaces.IProjectApplyService;
import com.itboye.services.recruit.interfaces.IProjectService;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.helper.ByConsoleHelper;
import com.itboye.system.helper.ByDateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * ProjectDomain.java
 * Created on  2016/8/10 08:57
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/8/10      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
@Component("ProjectDomain")
public class ProjectDomain extends BaseDomain {

    private static final Logger logger = LogManager.getLogger(ProjectDomain.class.toString());

    public ProjectDomain(){
        super();
        ByConsoleHelper.debug("ProjectDomain init",logger);
    }

    @Resource
    private IProjectService projectService;

    @Resource
    private IProjectApplyService projectApplyService;


    public ByResponseData query() throws ByBaseException, UnsupportedEncodingException {

        this.checkApiVersion("100");

        Integer pageNum = this.getIntegerParam("page_num");
        Integer pageSize = this.getIntegerParam("page_size");
        String title = this.getStringParam("keyword");

        String fields = null;
        String orders = null;

        HashMap<String,String> params = new HashMap<String, String>();
        params.put("title",title);

        ByPageInfoHelper<ProjectModel> projectModelPage = projectService.queryByPaging(params,pageNum,pageSize,fields,orders);

        return success(projectModelPage);
    }


    public ByResponseData apply() throws ByBaseException, UnsupportedEncodingException {

        this.checkApiVersion("100");

        Integer projectId = getIntegerParam("project_id");
        Integer uid  = getIntegerParam("uid");
        assertNotNull(projectId,"项目id不能为空");
        assertNotNull(uid,"uid不能为空");

        ProjectApplyModel applyModel = new ProjectApplyModel();
        applyModel.setProjectId(projectId);
        applyModel.setUid(uid);
        applyModel.setApplyTime(ByDateHelper.getCurrentTimeStamp().intValue());

        int effectRows =  projectApplyService.insert(applyModel);

        if(effectRows == 1){
            return success("操作成功");
        }

        return fail("操作失败");
    }

}
